﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok.Agents
{
	/// <summary>
	/// This type provides extension methods for <see cref="IAgentFactory"/>.
	/// </summary>
	public static class AgentFactoryExtensions
	{
		#region SpawnAgent(Type, AgentEnvironment)

		/// <summary>
		/// Spawns a new instance of the specified agent type.
		/// </summary>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="agentType">
		/// The type of agent to spawn. This must be a sub-class of <see cref="Agent"/>.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new agent instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned agent instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type must be a sub-class of <see cref="Agent"/>.</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type cannot be instantiated, because the agent type does not 
		/// provide an agent-specific constructor that accepts agent environment and agent options. Provide 
		/// a constructor with following signature: Agent(AgentEnvironment, AgentOptions).</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IAgent SpawnAgent(this IAgentFactory factory, Type agentType, AgentEnvironment environment)
		{
			return factory.SpawnAgent(
				agentType,
				environment,
				AgentOptions.Empty);
		}

		#endregion

		#region SpawnAgent<T>(AgentEnvironment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the specified agent type.
		/// </summary>
		/// 
		/// <typeparam name="TAgent">
		/// The type of agent to spawn. This must be a sub-class of <see cref="Agent"/>.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new agent instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new agent instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned agent instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type cannot be instantiated, because the agent type does not 
		/// provide an agent-specific constructor that accepts agent environment and agent options. Provide 
		/// a constructor with following signature: Agent(AgentEnvironment, AgentOptions).</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IAgent SpawnAgent<TAgent>(this IAgentFactory factory, AgentEnvironment environment, AgentOptions options)
			where TAgent : Agent
		{
			return factory.SpawnAgent(
				typeof(TAgent),
				environment,
				options);
		}

		#endregion

		#region SpawnAgent<T>(AgentEnvironment)

		/// <summary>
		/// Spawns a new instance of the specified agent type.
		/// </summary>
		/// 
		/// <typeparam name="TAgent">
		/// The type of agent to spawn. This must be a sub-class of <see cref="Agent"/>.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new agent instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned agent instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type cannot be instantiated, because the agent type does not 
		/// provide an agent-specific constructor that accepts agent environment and agent options. Provide 
		/// a constructor with following signature: Agent(AgentEnvironment, AgentOptions).</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IAgent SpawnAgent<TAgent>(this IAgentFactory factory, AgentEnvironment environment)
			where TAgent : Agent
		{
			return factory.SpawnAgent(
				typeof(TAgent),
				environment,
				AgentOptions.Empty);
		}

		#endregion

		#region SpawnAgent<T, E, O>(AgentEnvironment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the specified agent type.
		/// </summary>
		/// 
		/// <typeparam name="TAgent">
		/// The type of agent to spawn. This must be a sub-class of <see cref="Agent"/>.</typeparam>
		/// <typeparam name="TEnvironment">
		/// The agent environment to be supplied to the new agent instance.</typeparam>
		/// <typeparam name="TOptions">
		/// The agent options to be supplied to the new agent instance.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new agent instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new agent instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned agent instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type cannot be instantiated, because the agent type does not 
		/// provide an agent-specific constructor that accepts agent environment and agent options. Provide 
		/// a constructor with following signature: Agent(AgentEnvironment, AgentOptions).</exception>
		/// <exception cref="AgentSpawnException">
		/// An instance of the specified agent type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IAgent SpawnAgent<TAgent, TEnvironment, TOptions>(this IAgentFactory factory, TEnvironment environment, TOptions options)
			where TAgent : Agent<TEnvironment, TOptions>
			where TEnvironment : AgentEnvironment
			where TOptions : AgentOptions
		{
			return factory.SpawnAgent(
				typeof(TAgent),
				environment,
				options);
		}

		#endregion

	}
}
